/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scrumpmt.web;

import java.util.Date;
import java.util.logging.Level;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.scrumpmt.domain.Sprint;
import org.scrumpmt.ejb.SprintFacade;

/**
 *
 * @author aljiru
 */
@ManagedBean
@RequestScoped
public class NuevoSprintBean extends BasicBean {

    private SprintFacade sprintFacade = lookupSprintFacade();
    @ManagedProperty(value = "#{param.creado}")
    private String creado;
    FacesContext context = FacesContext.getCurrentInstance();
    ProductoSeleccionadoBean bean = (ProductoSeleccionadoBean) context.getApplication().
            evaluateExpressionGet(context, "#{productoSeleccionadoBean}", ProductoSeleccionadoBean.class);

    /**
     * Creates a new instance of NuevoSprintBean
     */
    public NuevoSprintBean() {
    }

    public String getCreado() {
        return creado;
    }

    public void setCreado(String creado) {
        this.creado = creado;
    }

    @PostConstruct
    public void cargaChunga() {
        if (creado != null && !creado.equals("")) {

            String id = bean.getProductoActual();
            Sprint sprint = sprintFacade.findSprintActualProyecto(id, new Date());
            String idsprint = String.valueOf(sprint.getId());
            bean.setSprintActual(idsprint);

            log.log(Level.INFO, "Producto:{0} - Sprint: {1}", new Object[]{id, idsprint});
        }
    }
    
    private SprintFacade lookupSprintFacade() {
        try {
            Context c = new InitialContext();
            return (SprintFacade) c.lookup("java:global/ScrumPMT/ScrumPMT-ejb/SprintFacade!org.scrumpmt.ejb.SprintFacade");
        } catch (NamingException ne) {
            log.log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
